package com.winit.datasync.service.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.winit.datasync.dao.UpsCustomerMerchSaleDao;
import com.winit.datasync.entity.UpsCustomerMerchSale;
import com.winit.datasync.service.UpsCustomerMerchSaleService;

@Service("upsCustomerMerchSaleService")
public class UpsCustomerMerchSaleServiceImpl implements UpsCustomerMerchSaleService {

	@Autowired
	private UpsCustomerMerchSaleDao upsCustomerMerchSaleDao;

	@Transactional
	@Override
	public void batchSave(List<UpsCustomerMerchSale> list, int datasouceType) throws Exception {
		if (null == list || list.size() == 0)
			return;

		if (datasouceType == 2) {
			for (UpsCustomerMerchSale upsCustomerMerchSale : list) {
				upsCustomerMerchSaleDao.delete(upsCustomerMerchSale, datasouceType);
			}
		} else if (datasouceType == 1) {
			upsCustomerMerchSaleDao.delete(null, datasouceType);
		}

		upsCustomerMerchSaleDao.batchInsert(list, datasouceType);

	}

	@Override
	public List<UpsCustomerMerchSale> getByParams(HashMap<String, String> params, int datasouceType)
			throws Exception {
		return upsCustomerMerchSaleDao.getByParams(params, datasouceType);
	}

	@Override
	public List<UpsCustomerMerchSale> getAll(int datasouceType) throws Exception {
		return upsCustomerMerchSaleDao.getAll(datasouceType);
	}

	@Override
	public void batchMongoSave(List<UpsCustomerMerchSale> newList,
			List<UpsCustomerMerchSale> existList) throws Exception {
		
		//1.删除新增加的
		for (UpsCustomerMerchSale upsCustomerMerchSale : newList) {
			upsCustomerMerchSaleDao.delete(upsCustomerMerchSale, 2);
		}
		//2.删除已存在的
		for (UpsCustomerMerchSale upsCustomerMerchSale : existList) {
			upsCustomerMerchSaleDao.delete(upsCustomerMerchSale, 2);
		}
		
		//3.还原已有的
		upsCustomerMerchSaleDao.batchInsert(existList, 2);
	}

}
